import Axios from 'axios'
import { getTestBaseUrl } from '@/utils/requestTestHeaderUtil'
import { getHeaders } from '@/utils/requestHeaderUtil'

const axios = Axios.create({
  timeout: 50000
})

axios.interceptors.request.use(
  (req) => {
    req.url = `${getTestBaseUrl()}${req.url}`
    const headers = getHeaders()
    if (headers) {
      headers.forEach(header => {
        if (header.name.replace(/\s/g, '') && header.val.replace(/\s/g, '')) {
          req.headers[header.name] = header.val
        }
      })
    }
    return req
  },
  (error) => {
    return Promise.reject(error)
  }
)

// http response 拦截器
axios.interceptors.response.use(
  (resp) => {
    return resp.data
  },
  (error) => {
    return Promise.reject(error.response) // 返回接口返回的错误信息
  }
)

export default axios
